/**
 * ...
 * @author Thot
 */

function MapMediator( p_view )
{
	this.init( MapMediator.NAME, p_view );
}

POO.extend( MapMediator, Mediator );


	// static
	MapMediator.NAME = "MapMediator";

	//private
	MapMediator.prototype._screen = null;
	
	
	// public
	MapMediator.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [p_name, p_data] );
		this.initRenderingContext();
		this.listenNotifications();	
	};
	
	MapMediator.prototype.initRenderingContext = function()
	{
		this._screen = new MapScreen();
		var container = this.getViewComponent();
		this._screen.init( container, 800, 600 );
	};
	
	MapMediator.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.LOAD_DATA_COMPLETE, 	this.onNotification );
		this.addNotificationListener( ApplicationNotification.LOAD_DATA_PROGRESS, 	this.onNotification );
		this.addNotificationListener( ApplicationNotification.ADD_ANIMATION, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.ADD_LAYER, 			this.onNotification );
		this.addNotificationListener( ApplicationNotification.SELECT_LAYER, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.REMOVE_LAYER, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.CLEAN_MAP, 			this.onNotification );
		this.addNotificationListener( ApplicationNotification.DISPLAY_MAP_ANIM, 	this.onNotification );
		this._screen.signal.addListener( MapScreen.ADD_ANIM, this.addAnimHandler, 	this );
	};
	
	MapMediator.prototype.addAnimHandler = function(p_data)
	{
		var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var animId = proxy.currentAnim;
		var layerId = proxy.currentLayer;
		
		if( animId == "" )
			return;
		
		this.getFacade().sendNotification( ApplicationNotification.ADD_ANIMATION, {"layerId":layerId,"animId":animId,"x":p_data.x,"y":p_data.y} );
	};

	MapMediator.prototype.onNotification = function( p_notificationType, p_data )
	{
		var animProxy = this.getFacade().getProxy( AnimationProxy.NAME );
		var appProxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var mapProxy = this.getFacade().getProxy( MapProxy.NAME );
		
		switch ( p_notificationType )
		{
			case ApplicationNotification.LOAD_DATA_COMPLETE:
				this.getFacade().sendNotification( ApplicationNotification.LOAD_MAPS_DATA );
				break;
				
			case ApplicationNotification.LOAD_DATA_PROGRESS:
				this._screen.showProgression( p_data.progression );
				break;
				
			case ApplicationNotification.ADD_ANIMATION:
				var animVO = animProxy.getAnimationVOById(p_data.animId);
				this._screen.addSpriteAt( animVO, appProxy.currentLayer, p_data.x, p_data.y );
				break;
				
			case ApplicationNotification.ADD_LAYER:
				this._screen.addLayer( p_data );
				break;
				
			case ApplicationNotification.REMOVE_LAYER:
				this._screen.removeLayer( appProxy.currentLayer );
				break;
				
			case ApplicationNotification.CLEAN_MAP:
				this._screen.clean();
				break;
				
			case ApplicationNotification.DISPLAY_MAP_ANIM:
				
				var vo = mapProxy.getMapVOById(appProxy.currentMap);
				var layerVO = null;
				var i = 0;
				var j = 0;
				var max = vo.layers.length;
				var max2 = 0;
				var animVO = null;
				var spriteData = null;
				
				for( i = 0; i < max ; i++ )
				{
					layerVO = vo.layers[i];
					this._screen.addLayer( layerVO.name );
					
					max2 = layerVO.displayList.length;
					
					for( j = 0; j < max2; j++ )
					{
						spriteData = layerVO.displayList[j];
						animVO = animProxy.getAnimationVOById(spriteData[7]);
						this._screen.addSpriteAt( animVO, layerVO.name, spriteData[0], spriteData[1] );
					}
				}
				
				break;
		}
	};
